<?php
session_start();
include ('../../configs/cms.php');
include ('../includes/index.php');

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	if ($_POST['which'] == 'choose1')
	{
		$query1 = mysql_query("SELECT titel, parentid, inhoud, bgimage, `active` FROM pagina WHERE `pagina_id` = '" . $_POST['pagina'] . "';");
		$result1 = mysql_fetch_assoc($query1);
		$sChecked = ''; if ($result1 ['active'] == 1) $sChecked = 'checked="checked"';
		?>
		<script type="text/javascript" src="includes/ckeditor/ckeditor.js"></script>
		<div style="margin-left: 40px;"><form method="POST" action="wijzigpagina.php">
		Titel:<br>
		<input type="text" name="titel" size="20" maxlength="255" value="<?php echo $result1['titel']; ?>"><br><br>
		Onder welke pagina:<br />
		<?php
		$query = mysql_query("SELECT pagina_id, titel, parentid FROM pagina;");
		echo '<select name="parentid">';
		echo '<option value="0">&nbsp;</option>' . "\n";
		while($result = mysql_fetch_assoc($query))
		{
			if ($result1['parentid'] == $result['pagina_id'])
			{
				echo '<option value="' . $result['pagina_id'] . '" selected="selected">' . $result['titel'] . '</option>' . "\n";
			}
			else
			{
				echo '<option value="' . $result['pagina_id'] . '">' . $result['titel'] . '</option>' . "\n";
			}
		}
		echo '</select>';
		?>
		<br /><br />
		Inhoud:<br>
		<textarea class="ckeditor" name="inhoud" id="inhoud" rows="30" cols="100" wrap="soft" maxlength="1500"><?php echo $result1['inhoud']; ?></textarea>
		<script type="text/javascript">
		CKEDITOR.replace( 'inhoud' );
		</script>
		<br><br>
		Achtergrondafbeelding:<br>
		<input type="text" name="bgimage" size="30" maxlength="254" value="<?php echo $result1['bgimage']; ?>">
		<br /><br />
		Zichtbaar:<br />
		<input name="iVisibility" type="checkbox" <?php echo $sChecked; ?>>
		<br /><br />
		<input type="hidden" name="which" value="choose2">
		<input type="hidden" name="pagina_id" value="<?php echo $_POST['pagina']; ?>">
		<input type="submit" value="Sla gewijzigde pagina op">&nbsp;&nbsp;<input type="reset" value="Leeg formulier">
		</form></div><?php
	}
	elseif ($_POST['which'] == 'choose2')
	{
		$titel = $_POST['titel'];
		$inhoud = $_POST['inhoud'];
		$parentid = $_POST['parentid'];
		$pagina_id = $_POST['pagina_id'];
		$bgimage = $_POST['bgimage'];
		if (isset ($_POST ['iVisibility'])) $iVisibility = 1;
		else $iVisibility = 0;

		if (strlen($titel) > 255 || empty($titel))
		{
			echo 'De titel is te lang of niet ingevuld<br>
			<a href="#back" onclick="history.go(-1)">Ga terug</a> om het te wijzigen.';
		}
		elseif (strlen($bgimage) > 255)
		{
			echo 'De achtergrondafbeeldingverwijzing is te lang of niet ingevuld<br>
			<a href="#back" onclick="history.go(-1)">Ga terug</a> om het te wijzigen.';
		}
		elseif (strlen(trim(strip_tags($inhoud))) > 1500 || !trim(strip_tags($inhoud)))
		{
			echo 'Je hebt te veel tekst geplaatst (max 1500 tekens) of niets ingevuld<br>
			<a href="#back" onclick="history.go(-1)">Ga terug</a> om het te wijzigen.';
		}
		else
		{
			$sql = "UPDATE `pagina` SET `titel` = '" . $titel . "', `inhoud` = '" . mysql_real_escape_string ($inhoud) . "', `parentid` = '" . $parentid . "', `bgimage` = '" . $bgimage . "', `active` = '" . $iVisibility . "' WHERE `pagina_id` = '" . $pagina_id . "';";
			mysql_query($sql);
			echo 'De pagina ' . $titel . ' is met succes gewijzigd.';

		}
	}
}
else
{
?>
	<div style="margin-left: 40px;"><form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	Welke pagina:<br />
	<?php
	$query = mysql_query ("SELECT pagina_id, titel FROM pagina;");
	$result = mysql_fetch_assoc (mysql_query ("SELECT COUNT(`pagina_id`) FROM `pagina`;"));
	echo '<select name="pagina" size="' . $result['COUNT(`pagina_id`)'] . '">';
	while ($result = mysql_fetch_assoc ($query))
	{
		echo '<option value="' . $result['pagina_id'] . '">' . $result['titel'] . '</option>' . "\n";
	}
	echo '</select>';
	?>
	<br /><br />
	<input type="hidden" name="which" value="choose1">
	<input type="submit" value="Wijzig pagina">&nbsp;&nbsp;<input type="reset" value="Leeg formulier"></form></div>
<?php
}